<?php
/**
 * tvb log config
 * 
 * get log config info
 * @author William Hu <william.hu@achievo.com>
 * @version 1.0
 * @package TVBLog
 */
class TVB_Logger_Config
{
	private static $_info = array();
	private static $_label = '';
	/**
     * get config.
     *
	 * @param string $path
     * @return vod
     */		
	public static function init($path)
	{
		$apckey_info = $path;
		$data = apc_fetch($apckey_info);
		//apc_delete($apckey_info);
		$level_name = LEVEL_NAME;
		$appender_name = APPENDER_NAME;
		$class_name = CLASS_NAME;
		$lib_path = LIP_PATH;
		if (!$data) {
			$config_xml = simplexml_load_file($path);
			foreach($config_xml as $key => $val) {
				$level = isset($val->$level_name) && !empty($val->$level_name) ? $val->$level_name : DEFAULT_APPENDER;
				$level_arr = explode(',', $level);
				$info['level'][$key] = $level_arr;
				$label[$key] = $key;
				$appender = isset($val->$appender_name) && !empty($val->$appender_name) ? $val->$appender_name : DEFAULT_APPENDER;
				$appender_arr = explode(',', $appender);
				$info['appender'][$key] = $appender_arr;
				foreach ($appender_arr as $val_a) {
					$out = isset($val->$val_a) && !empty($val->$val_a) ? $val->$val_a : '';
					$info['outclass'][$key][$val_a] = (string) $out->$class_name;
					$info['libpath'][$key][$val_a] = (string) $out->$lib_path;
					$info[$val_a][$key] = (array) $out;
				}
			}
			$data['info'] = self::object2array($info);
			$data['label'] = $label;
			//error_log(print_r($data,1));
			apc_store($apckey_info, $data, CONFIG_APC_TLL);
		}
		//error_log(print_r($data['label'],1));
		self::$_info = isset($data['info'])?$data['info']:array();
		self::$_label = isset($data['label'])?$data['label']:array();
	}

	/**
     * get appender.
     *
     * @return array
     */	
	public static function getAppender($label)
	{
		if (is_array(self::$_info['appender'][$label])) {
			foreach (self::$_info['appender'][$label] as $val) {
				if (isset(self::$_info['outclass'][$label][$val])) {
					require_once 'TVB/Logger/Appender.php';
					$libpath = isset(self::$_info['libpath'][$label][$val]) && !empty(self::$_info['libpath'][$label][$val])?self::$_info['libpath'][$label][$val]:CLASS_PATH;
					$appender[] = TVB_Logger_Appender::get(self::$_info['outclass'][$label][$val], $libpath);
				}
			}
		}
		return $appender;
	}
	/**
     * get label.
     *
     * @return array
     */		
	public static function labels()
	{
		$result = self::$_label;;
		return $result;
	}

	/**
     * get info.
     *
	 * @param string $label
     * @return array
     */
	public static function get($val, $label)
	{
		if (empty($val) || empty($label)) {
			require_once 'TVB/Logger/Exception.php';
			throw new TVB_Logger_Exception('empty format or empty category');
		}
		$key = strtolower($label);
		$result = self::$_info[$val];
		if (!isset($result[$key])) {
			require_once 'TVB/Logger/Exception.php';
			throw new TVB_Logger_Exception('no label '.$key);
		}
		return $result[$key];
	}
	
	public static function object2array($object)
	{
		$return = NULL;  
		if(is_array($object)){
			foreach($object as $key => $value){
				$return[$key] = self::object2array($value);
			}
		} else {
			if(is_object($object)){
				$var = get_object_vars($object);
				if($var) {
					foreach($var as $key => $value){
						$return[$key] = ($key && !$value) ? NULL : self::object2array($value);
					}
				}
			} else {
				return $object;
			}
		}
		return $return;
	} 
}
?>
